package com.xxx.lease.web.app.service.impl;

import com.xxx.lease.common.context.LoginUserContext;
import com.xxx.lease.model.entity.*;
import com.xxx.lease.model.enums.ItemType;
import com.xxx.lease.model.enums.LeaseStatus;
import com.xxx.lease.web.app.mapper.*;
import com.xxx.lease.web.app.service.ApartmentInfoService;
import com.xxx.lease.web.app.service.BrowsingHistoryService;
import com.xxx.lease.web.app.service.GraphInfoService;
import com.xxx.lease.web.app.service.RoomInfoService;
import com.xxx.lease.web.app.vo.apartment.ApartmentItemVo;
import com.xxx.lease.web.app.vo.attr.AttrValueVo;
import com.xxx.lease.web.app.vo.fee.FeeValueVo;
import com.xxx.lease.web.app.vo.graph.GraphVo;
import com.xxx.lease.web.app.vo.room.RoomDetailVo;
import com.xxx.lease.web.app.vo.room.RoomItemVo;
import com.xxx.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;


    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(roomItemVoPage, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {

        RoomInfo roomInfo = super.getById(id);

        ApartmentItemVo apartmentItemVo = apartmentInfoService.getApartmentItemVoById(roomInfo.getApartmentId());


        List<GraphVo> graphVoList = graphInfoMapper.selectGraphInfoByIdAndType(id, ItemType.ROOM);

        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoByRoomId(id);

        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoByRoomId(id);

        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoByRoomId(id);

        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeByRoomId(id);

        List<FeeValueVo> feeValueVoList =  feeValueMapper.selectFeeValueVoByRoomId(roomInfo.getApartmentId());

        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermByRoomId(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();

        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        roomDetailVo.setApartmentItemVo(apartmentItemVo);

        roomDetailVo.setGraphVoList(graphVoList);

        roomDetailVo.setAttrValueVoList(attrValueVoList);

        roomDetailVo.setFacilityInfoList(facilityInfoList);

        roomDetailVo.setLabelInfoList(labelInfoList);

        roomDetailVo.setPaymentTypeList(paymentTypeList);

        roomDetailVo.setFeeValueVoList(feeValueVoList);

        roomDetailVo.setLeaseTermList(leaseTermList);

        Long count = leaseAgreementMapper.selectCount(new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING));

        roomDetailVo.setIsCheckIn(count > 0);

        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);

        //保存浏览历史的信息
        browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(), id);


        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(Page<RoomItemVo> roomItemVoPage, Long id) {
        return roomInfoMapper.pageItemByApartmentId(roomItemVoPage, id);
    }
}




